# doomlegacy
# Builds from https://github.com/puppylinux-woof-CE/petbuilds
# File auto generated by ./mk_petbuild.sh. Edit as necessary.

. ../func
. ../build.conf

URL=https://download.sourceforge.net/project/doomlegacy/1.46.3
PKG=doomlegacy
VER=1.46.3
COMP=tar.bz2
GVER=0.11.2
GURL=https://github.com/freedoom/freedoom/releases/download/v${GVER}/
GAME=freedoom
ZCOMP=zip
DESC="doom legacy is a shooter based on the original doom"
DEPS=+sdl
CAT=Fun
DESKTOP=doomlegacy.desktop
BUILD=
CWD=$(pwd)
[ -z "$MWD" ] && MWD=$(dirname $CWD)
[ -z "$BUILD" ] && BUILD=$DEF_BUILD
# comment these 2 below if you use a custom package() function
#[ -f "$DESKTOP" ] || DESKTOP=not
#[ -z "$DEPS" ] && DEPS=not

ARCH=$(uname -m)
case $ARCH in # set in build.conf
 *64) 	LIBDIR=$LIBCONF64 
		FLAGS=$CF64;;
 arm*)	LIBDIR=$LIBCONFarm
		FLAGS="-O2" ;;
 i686) 	LIBDIR=$LIBCONF32 
		FLAGS=$CF32;;
	*) 	LIBDIR=$LIBCONF32 
		FLAGS="-O2 -march=$ARCH -mtune=$ARCH";;
esac

extract_zip() {
	unzip ${1}
	[ "$?" -eq 0 ] || exit
	mv -f ${1} ../0sources/
}

build_() {
	mv -f ${PKG}_${VER}_source ${PKG}-${VER}
	(
	cd ${PKG}-${VER}
	make || exit
	)
	install -d -m 0755 ${PKG}-install/usr/{games,share}
	install -d -m 0755 ${PKG}-install/usr/share/{applications,pixmaps,doc}
	install -s -m 0755 ${PKG}-${VER}/bin/${PKG} ${PKG}-install/usr/games
	install -m 0644 ${PKG}_${VER}/legacy.wad ${PKG}-install/usr/games
	install -m 0644 ${PKG}-${VER}/docs/LICENSE.txt ${PKG}-install/usr/share/doc/LICENCE-doomlegacy.txt
	install -m 0644 ${GAME}-${GVER}/*.wad ${PKG}-install/usr/games
	install -m 0644 ${GAME}-${GVER}/COPYING.txt ${PKG}-install/usr/share/doc/LICENCE-freedoom.txt
	install -m 0644 ${PKG}.png ${PKG}-install/usr/share/pixmaps/
}

package_() {
	# strip
	find ${PKG}-install | xargs file | grep -e "executable" -e "shared object" \
		| grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	# copy
	cp -arf ${PKG}-install ${PKG}-${VER}-${ARCH}_${BUILD}
	mkdir -p ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/src/dev
	# add this recipe
	cat ${PKG}.petbuild > ./${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/src/dev/${PKG}-build-recipe
	# add custom .desktop
	if [ -f "$DESKTOP" ];then
		cat $DESKTOP > ./${PKG}-${VER}-${ARCH}_${BUILD}/usr/share/applications/$DESKTOP
	fi
	# split
	#get_files ${PKG}-install ${PKG}-${VER}-${ARCH}_${BUILD}
	#(cd ${PKG}-${VER}; $MWD/split.sh ../${PKG}-install $BUILD)
	# delete any icon cache or library cache
	find ./${PKG}-${VER}-${ARCH}_${BUILD} -type f -name '*cache' -delete
	# add pinstalls if exists
	[ -f ./pinstall.sh ] && install -m 0755 pinstall.sh ./${PKG}-${VER}-${ARCH}_${BUILD}/
	[ -f ./puninstall.sh ] && install -m 0755 puninstall.sh ./${PKG}-${VER}-${ARCH}_${BUILD}/
	for p in $(ls|grep "\-${ARCH}"|grep -v "files$") ; do
		case $p in
			*_DEV*) DESC="$PKG development"; DEPS=+${PKG} ;;
			*_DOC*) DESC="$PKG documentation"; DEPS=+${PKG} ;;
			*_NLS*) DESC="$PKG locales"; DEPS=+${PKG} ;;
		esac	
		echo "packaging $p"	
		dir2pet -x -s -w="$DESC" -d="$DEPS" -c="$CAT" -p=${p} 2>&1 >/dev/null
		rm -r $p
		mv ${p}.pet ../0pets_out/
	done
	rm -r ${PKG}-install
	rm -r ${PKG}-${VER}
	rm -r ${PKG}_${VER}
	rm -r ${GAME}-${GVER}
	#mv -f *.files ../0logs/
}

# main
retrieve ${PKG}_${VER}_source.${COMP}
retrieve ${PKG}_${VER}_common.${ZCOMP}
URL=$GURL
retrieve ${GAME}-${GVER}.${ZCOMP}
extract ${PKG}_${VER}_source.${COMP}
extract_zip ${PKG}_${VER}_common.${ZCOMP}
extract_zip ${GAME}-${GVER}.${ZCOMP}
build_
package_
